/*
 * Copyright (C) 2003, Velocityme Partnership
 * TimeSummaryTable.java
 *
 * Created on 12 November 2004, 04:52
 */

package com.velocityme.utility;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author  Robert
 */
public class TimeSummaryTable implements Serializable {
    
    private String m_name;
    private Collection<Row> m_rows;
    private long[] m_maxes;
    private long m_max;
    
    public class Row implements Serializable {
        private String m_name;
        private LongPercent[] m_longPercents;
        
        public class LongPercent implements Serializable {
            private long m_long;
            private int m_percent;
            
            public LongPercent(long l) {
                m_long = l;
                m_percent = 0;
            }
            public long getLong() { return m_long; }
            public int getPercent() { return m_percent; }
            public void setMax(long max) { m_percent = (int)(m_long*100L/Math.max(max, 1L)); }
        }
        
        public Row(String name, Long[] longs) {
            m_name = name;
            m_longPercents = new LongPercent[longs.length];
            m_max = 0L;
            for (int i = 0; i < longs.length; i++)
                m_longPercents[i] = new LongPercent(longs[i].longValue());
        }
        
        public String getName() { return m_name; }
        public LongPercent[] getLongPercents() { return m_longPercents; }
        public long getMax() { return m_max; }
        public void setMax(long[] maxes) {
            m_max = 0L;
            for (int i=0; i < maxes.length; i++) {
                m_longPercents[i].setMax(maxes[i]);
                // find max of all columns except total
                if (i != maxes.length - 1)
                    m_max = Math.max(m_max, maxes[i]);
            }
        }
    }
    
    /** Creates a new instance of TimeSummaryTable */
    public TimeSummaryTable(String name) {
        m_name = name;
        m_rows = new ArrayList<Row>();
    }
    
    public void addRow(String name, Long[] longs) {
        if (m_maxes == null)
            m_maxes = new long[longs.length];
        Row newRow = new Row(name, longs);
        m_rows.add(newRow);
        for (int i = 0; i < longs.length; i++)
            m_maxes[i] = Math.max(m_maxes[i], longs[i].longValue());
        for (Row row: m_rows) {
            row.setMax(m_maxes);
        }
    }
    
    public String getName() {
        return m_name;
    }
    
    public Collection<Row> getRows() {
        return m_rows;
    }
    
    public long getMax() {
        return m_max;
    }
    
}
